package com.zhb.lkd.service.impl;

import com.zhb.lkd.mapper.TaskTypeMapper;
import com.zhb.lkd.pojos.entity.Task;
import com.zhb.lkd.mapper.TaskMapper;
import com.zhb.lkd.pojos.entity.TaskType;
import com.zhb.lkd.pojos.vo.taskReportInfoVo;
import com.zhb.lkd.service.ITaskService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

/**
 * <p>
 * 工单表 服务实现类
 * </p>
 *
 * @author haibin
 * @since 2024-08-18
 */
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements ITaskService {
    @Resource
    private TaskTypeMapper taskTypeMapper;

    /**
     * 工单统计
     *
     * @param start
     * @param end
     * @return 响应数据是一个数组，内部包含两类（运营，运维）工单的统计情况。
     */
    @Override
    public List<taskReportInfoVo> taskReportInfo(LocalDateTime start, LocalDateTime end) {
        //1.检查数据是否正确
        if (start == null || end == null) {
            return List.of();
        }
        //2.数据封装
        List<Task> list = lambdaQuery().between(Task::getCreateTime, start, end).list();
        List<TaskType> taskTypes = taskTypeMapper.selectList(null);
        List<taskReportInfoVo> taskReportInfoVo = toTaskReportInfoVo(list, taskTypes);
        //3.返回数据
        return taskReportInfoVo;
    }

    /**
     * 将task转换为taskReportInfoVo
     *
     * @param tasks
     * @param taskTypes
     * @return
     */
    public List<taskReportInfoVo> toTaskReportInfoVo(List<Task> tasks, List<TaskType> taskTypes) {
        //运营工单对象
        taskReportInfoVo yytaskReportInfoVo = new taskReportInfoVo();
        yytaskReportInfoVo.setTotal(0L);
        yytaskReportInfoVo.setCompletedTotal(0L);
        yytaskReportInfoVo.setCancelTotal(0L);
        yytaskReportInfoVo.setProgressTotal(0L);
        yytaskReportInfoVo.setWorkerCount(0L);
        yytaskReportInfoVo.setRepair(true);
        yytaskReportInfoVo.setDate(LocalDateTime.now());


        //维修工单对象
        taskReportInfoVo ywtaskReportInfoVo = new taskReportInfoVo();
        ywtaskReportInfoVo.setTotal(0L);
        ywtaskReportInfoVo.setCompletedTotal(0L);
        ywtaskReportInfoVo.setCancelTotal(0L);
        ywtaskReportInfoVo.setProgressTotal(0L);
        ywtaskReportInfoVo.setWorkerCount(0L);
        ywtaskReportInfoVo.setRepair(false);
        ywtaskReportInfoVo.setDate(LocalDateTime.now());
        //找出taskTypes中运营工单和维修工单
        //维修工单
        List<Integer> wxTaskTypeId = new ArrayList<>();
        for (TaskType taskType : taskTypes) {
            if (taskType.getType() == 1) {
                wxTaskTypeId.add(taskType.getTypeId());
            }
        }
        for (Task task : tasks) {
            //在维修工单id中就是维修工单
            int i = Collections.binarySearch(wxTaskTypeId, task.getProductTypeId());
            if (i >= 0) {
                ywtaskReportInfoVo.setTotal(ywtaskReportInfoVo.getTotal() + 1);
                if (task.getTaskStatus() == 4) {
                    ywtaskReportInfoVo.setCompletedTotal(ywtaskReportInfoVo.getCompletedTotal() + 1);
                } else if (task.getTaskStatus() == 3) {
                    ywtaskReportInfoVo.setCancelTotal(ywtaskReportInfoVo.getCancelTotal() + 1);
                } else if (task.getTaskStatus() == 2) {
                    ywtaskReportInfoVo.setProgressTotal(ywtaskReportInfoVo.getProgressTotal() + 1);
                }
            }
            //运维工单
            else {
                yytaskReportInfoVo.setTotal(yytaskReportInfoVo.getTotal() + 1);
                if (task.getTaskStatus() == 4) {
                    yytaskReportInfoVo.setCompletedTotal(yytaskReportInfoVo.getCompletedTotal() + 1);
                } else if (task.getTaskStatus() == 3) {
                    yytaskReportInfoVo.setCancelTotal(yytaskReportInfoVo.getCancelTotal() + 1);
                } else if (task.getTaskStatus() == 2) {
                    yytaskReportInfoVo.setProgressTotal(yytaskReportInfoVo.getProgressTotal() + 1);
                }
                yytaskReportInfoVo.setWorkerCount(0L);
            }
        }
        //封装数据
        List<taskReportInfoVo> taskReportInfoVos = new ArrayList<>();
        taskReportInfoVos.add(ywtaskReportInfoVo);
        taskReportInfoVos.add(yytaskReportInfoVo);
        return taskReportInfoVos;
    }
}
